<html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - pybind_utils.h</title></head><body bgcolor='white'><pre>
<font color='#009900'>// Copyright (C) 2013  Davis E. King (davis@dlib.net)
</font><font color='#009900'>// License: Boost Software License   See LICENSE.txt for the full license.
</font><font color='#0000FF'>#ifndef</font> DLIB_PYBIND_UtILS_Hh_
<font color='#0000FF'>#define</font> DLIB_PYBIND_UtILS_Hh_

<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>pybind11<font color='#5555FF'>/</font>pybind11.h<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>vector<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>string<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>dlib<font color='#5555FF'>/</font>serialize.h<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>array<font color='#5555FF'>&gt;</font>

<font color='#0000FF'>namespace</font> py <font color='#5555FF'>=</font> pybind11;

<font color='#0000FF'>namespace</font> dlib
<b>{</b>

    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>&gt;</font>
    std::vector<font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font> <b><a name='python_list_to_vector'></a>python_list_to_vector</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> py::list<font color='#5555FF'>&amp;</font> obj
    <font face='Lucida Console'>)</font>
    <font color='#009900'>/*!
        ensures
            - converts a python object into a std::vector&lt;T&gt; and returns it.
    !*/</font>
    <b>{</b>
        std::vector<font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font> <font color='#BB00BB'>vect</font><font face='Lucida Console'>(</font><font color='#BB00BB'>len</font><font face='Lucida Console'>(</font>obj<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> vect.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
        <b>{</b>
            vect[i] <font color='#5555FF'>=</font> obj[i].cast<font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <b>}</b>
        <font color='#0000FF'>return</font> vect;
    <b>}</b>

    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font><font color='#0000FF'>typename</font> T,<font color='#0000FF'><u>size_t</u></font> N<font color='#5555FF'>&gt;</font>
    std::array<font color='#5555FF'>&lt;</font>T,N<font color='#5555FF'>&gt;</font> <b><a name='python_list_to_array'></a>python_list_to_array</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> py::list<font color='#5555FF'>&amp;</font> the_list
    <font face='Lucida Console'>)</font>
    <font color='#009900'>/*!
        ensures
            - converts a python object into a std::array&lt;T,N&gt; and returns it.
    !*/</font>
    <b>{</b>
        <font color='#BB00BB'>DLIB_CASSERT</font><font face='Lucida Console'>(</font><font color='#BB00BB'>len</font><font face='Lucida Console'>(</font>the_list<font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> N, "<font color='#CC0000'>Expected a list of </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> N <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'> things.</font>"<font face='Lucida Console'>)</font>;
        std::array<font color='#5555FF'>&lt;</font>T,N<font color='#5555FF'>&gt;</font> vect;
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> vect.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
        <b>{</b>
            vect[i] <font color='#5555FF'>=</font> the_list[i].cast<font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <b>}</b>
        <font color='#0000FF'>return</font> vect;
    <b>}</b>

    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>&gt;</font>
    py::list <b><a name='vector_to_python_list'></a>vector_to_python_list</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> std::vector<font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> vect
    <font face='Lucida Console'>)</font>
    <font color='#009900'>/*!
        ensures
            - converts a std::vector&lt;T&gt; into a python list object.
    !*/</font>
    <b>{</b>
        py::list obj;
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> vect.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
            obj.<font color='#BB00BB'>append</font><font face='Lucida Console'>(</font>vect[i]<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>return</font> obj;
    <b>}</b>

    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>void</u></font> <b><a name='extend_vector_with_python_list'></a>extend_vector_with_python_list</b> <font face='Lucida Console'>(</font>
        std::vector<font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font> <font color='#5555FF'>&amp;</font>v,
        <font color='#0000FF'>const</font> py::list <font color='#5555FF'>&amp;</font>l
    <font face='Lucida Console'>)</font>
    <font color='#009900'>/*!
        ensures
            - appends items from a python list to the end of std::vector&lt;T&gt;.
    !*/</font>
    <b>{</b>
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'>const</font> <font color='#0000FF'>auto</font> <font color='#5555FF'>&amp;</font>item : l<font face='Lucida Console'>)</font>
            v.<font color='#BB00BB'>push_back</font><font face='Lucida Console'>(</font>item.cast<font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font><font color='#0000FF'>typename</font> T<font color='#5555FF'>&gt;</font>
    std::shared_ptr<font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font> <b><a name='load_object_from_file'></a>load_object_from_file</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> std::string<font color='#5555FF'>&amp;</font> filename
    <font face='Lucida Console'>)</font>
    <font color='#009900'>/*!
        ensures
            - deserializes an object of type T from the given file and returns it.
    !*/</font>
    <b>{</b>
        std::ifstream <font color='#BB00BB'>fin</font><font face='Lucida Console'>(</font>filename.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, std::ios::binary<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#5555FF'>!</font>fin<font face='Lucida Console'>)</font>
            <font color='#0000FF'>throw</font> dlib::<font color='#BB00BB'>error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Unable to open </font>" <font color='#5555FF'>+</font> filename<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>auto</font> obj <font color='#5555FF'>=</font> std::make_shared<font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>deserialize</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font>obj, fin<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>return</font> obj;
    <b>}</b>
<b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>

<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_PYBIND_UtILS_Hh_
</font>

</pre></body></html>